package com.by.sbdemoby.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @auth wzy
 * @date 2023/12/9 9:43
 */
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

//        int i=1/0;

        HttpServletRequest request =  (HttpServletRequest)servletRequest;
        HttpServletResponse response =  (HttpServletResponse)servletResponse;

        Object loginUser = request.getSession().getAttribute("LoginUser");

        if (loginUser == null){
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print("请先进行登陆，再进行后续操作！(Filter控制))");
            return;
        }


        filterChain.doFilter(request,response);
    }
}
